package concept.chapter7.designusingcollectionclasses;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * In order to put an entry successfully into a Map, 
 * the key must be both equal and hashcode-identical. 
 * 
 * @author kan.sun
 *
 */
public class MapEQ
{
	public static void main(String[] args)
	{
		Map<ToDos, String> m = new HashMap<ToDos, String>();
		ToDos t1 = new ToDos("Monday");
		ToDos t2 = new ToDos("Monday");
		ToDos t3 = new ToDos("Tuesday");
		m.put(t1, "doLaundry");
		m.put(t2, "payBills");
		m.put(t3, "cleanAttic");
		
		System.out.println(m.size());

		for(Entry<ToDos, String> set : m.entrySet())
		{
			System.out.println(set);
		}
		System.out.println(m.get(t1));
	}
}

class ToDos
{
	String day;

	private final int HC;
	
	//calculate different hashcodes every time
	//an object is instantiated. 
	{
		try
		{
			Thread.sleep(50);
		} 
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
		HC = (int)new Date().getTime();
	}

	ToDos(String d)
	{
		day = d;
	}
	
	public String toString()
	{
		return day;
	}

/*	public boolean equals(Object o)
	{
		return ((ToDos) o).day.equals(this.day);
	}

	public int hashCode()
	{
		//return 9;
		return HC;
	}*/
}
